package com.cskaoyan.service.promote;

import com.cskaoyan.bean.common.BasePageInfoCouponList;
import com.cskaoyan.bean.po.MarketCoupon;
import com.cskaoyan.bean.po.MarketCouponUser;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: market-project
 * @BelongsPackage: com.cskaoyan.service.promote
 * @CreateTime: 2022-11-20  21:18
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    @Override
    public PageInfo<MarketCoupon> selectByBasePageInfo(BasePageInfoCouponList in) {
        PageHelper.startPage(in.getPage(), in.getLimit());
        //MarketCouponExample example = new MarketCouponExample();
        String name = in.getName();
        if (name != null) {
            in.setName(name.trim());
        }
        List<MarketCoupon> list = marketCouponMapper.selectByBasePageInfoCouponList(in);
        PageInfo<MarketCoupon> marketCouponPageInfo = new PageInfo<>(list);
        return marketCouponPageInfo;
    }

    @Override
    public void updateByMarketCoupon(MarketCoupon marketCoupon) {
        marketCouponMapper.updateByPrimaryKeySelective(marketCoupon);
    }

    @Override
    public void deleteByMarketCoupon(MarketCoupon marketCoupon) {
        marketCouponMapper.deleteByPrimaryKey(marketCoupon.getId());
    }

    @Override
    public MarketCoupon selectById(Integer id) {
        MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(id);
        return marketCoupon;
    }

    @Override
    public PageInfo<MarketCouponUser> selectByBasePageInfoCoupon(HttpServletRequest request) {
        Integer page = Integer.valueOf(request.getParameter("page"));
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        Integer couponId = Integer.valueOf(request.getParameter("couponId"));
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");
        PageHelper.startPage(page, limit);
        List<MarketCouponUser> marketCouponUsers = marketCouponUserMapper.selectByExample(null);
        PageInfo<MarketCouponUser> marketCouponUserPageInfo = new PageInfo<>(marketCouponUsers);
        return marketCouponUserPageInfo;
    }

    @Override
    public void insertByMarketCoupon(MarketCoupon marketCoupon) throws ParseException {
        if (marketCoupon.getType() == 2) {
            marketCoupon.setCode("123456");
        }
        long now = System.currentTimeMillis();
        if (marketCoupon.getEndTime() != null) {

            long endTime1 = marketCoupon.getEndTime().getTime();
            if (now > endTime1) {
                marketCoupon.setStatus((short) 2);
            }
        } else if (marketCoupon.getDays() != null) {
            SimpleDateFormat format = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
            Date date = format.parse(format.format(now));
            marketCoupon.setStartTime(date);
            Calendar ca = Calendar.getInstance();
            ca.setTime(date);
            ca.add(Calendar.DATE, marketCoupon.getDays());
            Date endTime = ca.getTime();
            marketCoupon.setEndTime(endTime);
            marketCoupon.setStatus((short) 0);
        }
        marketCouponMapper.insertSelective(marketCoupon);
    }

}